#include "widget.h"
#include "ui_widget.h"
#include <QTcpSocket>
#include <QSettings>
#include <QLibrary>
#include <QDebug>
#include <QDir>
#include <QVBoxLayout>
#include <QTimer>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
    , socket(new QTcpSocket(this))
{
    ui->setupUi(this);

    socket->connectToHost("127.0.0.1", 1234);

    QSettings settings("./config.ini", QSettings::IniFormat);
    edition = settings.value("edition/id").toFloat();
    //qDebug() << edition;

    connect(this->socket,&QTcpSocket::readyRead,this,&Widget::ready_read);

    this->ui->pushButton_2->setEnabled(false);

    this->update();

}

void Widget::ready_read() {
    qDebug() << "11111111111111111111";
    QByteArray newData = socket->readAll();
    accumulatedData.append(newData);

    if(this->filename==""){
        QTimer::singleShot(200, [=]() {
            ready_read();
        });
    }
    QFile file(filename);

    // 打开文件以写入
    if (!file.open(QIODevice::WriteOnly)) {
        qDebug() << "Failed to open file for writing";
        return;
    }
    // 检查结束标志
    if (accumulatedData.contains("END_OF_FILE")) {
        int endIndex = accumulatedData.indexOf("END_OF_FILE");
        QByteArray fileData = accumulatedData.left(endIndex);
        file.write(fileData);

        // 关闭文件
        file.close();
        qDebug() << "File received and saved";

        // 清理状态
        accumulatedData.clear();

        if(this->filename=="hot_refresh_demo_plugin.dll"){
            set_hot_refresh_demo_plugin();
            this->filename="";
        }
        if(this->filename=="hot_refresh_demo_plugin_2.dll"){
            set_hot_refresh_demo_plugin_2();
            this->filename="";
        }
    }
}

Widget::~Widget()
{
    delete ui;
    delete ww;
}

void Widget::on_pushButton_clicked()
{
    this->socket->write("hot_refresh_demo_plugin.dll");
    this->filename="hot_refresh_demo_plugin.dll";

    this->ui->pushButton->setEnabled(false);

}


void Widget::on_pushButton_2_clicked()
{
    this->socket->write("hot_refresh_demo_plugin_2.dll");
    this->filename="hot_refresh_demo_plugin_2.dll";

    this->ui->pushButton_2->setEnabled(false);
}

void Widget::set_hot_refresh_demo_plugin()
{

    QString path = "hot_refresh_demo_plugin.dll";

    qDebug()<<path;

    QLibrary dllLib;
    dllLib.setFileName(path);
    if(dllLib.load())
    {
        rootWidget wg= reinterpret_cast<rootWidget>(dllLib.resolve("rootWidget"));

        ww=wg();
        ww->resize(400,300);
        QVBoxLayout *layout = new QVBoxLayout(this->ui->plugin);
        layout->addWidget(ww);
    }

    this->ui->pushButton_2->setEnabled(true);
}

void Widget::set_hot_refresh_demo_plugin_2()
{

    QString path = "hot_refresh_demo_plugin_2.dll";

    qDebug()<<path;

    QLibrary dllLib;
    dllLib.setFileName(path);
    if(dllLib.load())
    {
        rootWidget wg= reinterpret_cast<rootWidget>(dllLib.resolve("rootWidget"));

        ww2=wg();
        ww2->resize(400,300);
        QVBoxLayout *layout = new QVBoxLayout(this->ui->plugin_2);
        layout->addWidget(ww2);
    }
}

